package com.ruoyi.gis.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gis.domain.GisDictType;
import com.ruoyi.gis.service.IGisDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 文件字典类型Controller
 *
 * @author rww
 * @date 2023-02-20
 */
@RestController
@RequestMapping("/gis/dict/type")
public class GisDictTypeController extends BaseController
{
    @Autowired
    private IGisDictTypeService gisDictTypeService;

    /**
     * 查询文件字典类型列表
     */
    @PreAuthorize("@ss.hasPermi('GisDictType:GisDictType:list')")
    @GetMapping("/list")
    public TableDataInfo list(GisDictType GisDictType)
    {
        startPage();
        List<GisDictType> list = gisDictTypeService.selectGisDictTypeList(GisDictType);
        return getDataTable(list);
    }

    /**
     * 导出文件字典类型列表
     */
    @PreAuthorize("@ss.hasPermi('GisDictType:GisDictType:export')")
    @Log(title = "文件字典类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GisDictType GisDictType)
    {
        List<GisDictType> list = gisDictTypeService.selectGisDictTypeList(GisDictType);
        ExcelUtil<GisDictType> util = new ExcelUtil<GisDictType>(GisDictType.class);
        util.exportExcel(response, list, "文件字典类型数据");
    }

    /**
     * 获取文件字典类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('GisDictType:GisDictType:query')")
    @GetMapping(value = "/{dictId}")
    public AjaxResult getInfo(@PathVariable("dictId") Long dictId)
    {
        return success(gisDictTypeService.selectGisDictTypeByDictId(dictId));
    }

    /**
     * 新增文件字典类型
     */
    @PreAuthorize("@ss.hasPermi('GisDictType:GisDictType:add')")
    @Log(title = "文件字典类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GisDictType dict)
    {
        if (UserConstants.NOT_UNIQUE.equals(gisDictTypeService.checkDictTypeUnique(dict)))
        {
            return error("新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dict.setCreateBy(getUsername());
        return toAjax(gisDictTypeService.insertGisDictType(dict));

    }

    /**
     * 修改文件字典类型
     */
    @PreAuthorize("@ss.hasPermi('GisDictType:GisDictType:edit')")
    @Log(title = "文件字典类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GisDictType dict)
    {

        if (UserConstants.NOT_UNIQUE.equals(gisDictTypeService.checkDictTypeUnique(dict)))
        {
            return error("修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dict.setUpdateBy(getUsername());
        return toAjax(gisDictTypeService.updateDictType(dict));

    }

    /**
     * 删除文件字典类型
     */
    @PreAuthorize("@ss.hasPermi('GisDictType:GisDictType:remove')")
    @Log(title = "文件字典类型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{dictIds}")
    public AjaxResult remove(@PathVariable Long[] dictIds)
    {
        return toAjax(gisDictTypeService.deleteGisDictTypeByDictIds(dictIds));
    }


    @PreAuthorize("@ss.hasPermi('GisDictType:GisDictType:remove')")
    @Log(title = "字典类型", businessType = BusinessType.CLEAN)
    @DeleteMapping("/refreshCache")
    public AjaxResult refreshCache()
    {
        gisDictTypeService.resetDictCache();
        return success();
    }

    /**
     * 获取字典选择框列表
     */
    @GetMapping("/optionselect")
    public AjaxResult optionselect()
    {
        List<GisDictType> dictTypes = gisDictTypeService.selectDictTypeAll();
        return success(dictTypes);
    }
}
